package cn.xu.servlet;

import java.io.IOException;
import java.io.InputStream;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

public class CityServlet extends HttpServlet {

	
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		
		response.setContentType("text/xml;charset=utf-8");
		
		String pname = request.getParameter("pname");
		pname = new String(pname.getBytes("iso-8859-1"),"utf-8");
		
		/*
		 * 接下xml 然后pname的值 来查找 指定省份下的 城市
		 */
		SAXReader saxReader = new SAXReader();
		InputStream input = this.getClass().getResourceAsStream("/china.xml");
		try {
			Document document = saxReader.read(input);
			
			Element ele = (Element)document.selectSingleNode("//province[@name='"+pname +"']");
			String xml = ele.asXML();
			response.getWriter().print(xml);
			
		} catch (DocumentException e) {
			throw new RuntimeException();
		} 
	}

}
